package cn.hospital.controller;

import cn.hospital.entity.AnnotationFields;
import cn.hospital.entity.Template;
import cn.hospital.service.QueryFieldAnnotationsService;
import cn.hospital.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;
    @Autowired
    private QueryFieldAnnotationsService queryFieldAnnotationsService;

    /**
     * 添加模板
     * @param title
     * @param field_name
     * @param type_of_frame
     * @param contents_of_the_box
     * @param session
     * @return
     */
    @RequestMapping("/insertTemplate")
    @ResponseBody
    public Map<String, Object> insertTemplate(String[] title, String[] field_name, String[] type_of_frame,
                                              String[] contents_of_the_box, String createName, HttpSession session) {
        templateService.insertSelective(title, field_name, type_of_frame, contents_of_the_box, createName, session);
        Map<String, Object> map = new HashMap<>();
        return map;
    }

    /**
     * 修改及添加之后的模板
     *
     * @param title
     * @param field_name
     * @param type_of_frame
     * @param contents_of_the_box
     * @param session
     * @return
     */
    @RequestMapping("/insertTemplates")
    @ResponseBody
    public Map<String, Object> insertTemplates(String[] title, String[] field_name, String[] type_of_frame,
                                               String[] contents_of_the_box, String createName,String updatePageId,
                                               String updateVersionNumber, HttpSession session) {
        templateService.insertSelectiveTemplatem(title, field_name, type_of_frame, contents_of_the_box,
                createName,updatePageId,updateVersionNumber, session);
        Map<String, Object> map = new HashMap<>();
        return map;
    }

    /**
     * 查询所有的模板数据
     * @return
     */
    @RequestMapping("/selectTemplate")
    @ResponseBody
    public Map<String, Object> selectTemplate(String pageNoStr, String pageSizeStr) {
        //获取参数
        long pageNo = 1; //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5; //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        Map<String, Object> mapList = new HashMap<String, Object>();
        mapList.put("templates", templateService.selectTemplate(map));
        mapList.put("count", templateService.selectTemplateCount(map));
        return mapList;
    }

    /**
     * 修改状态码
     *
     * @param pageId
     * @return
     */
    @RequestMapping("/updatTemplateState")
    public String updatTemplateState(String pageId, String versionNumber,String updatedDate) {
        templateService.updatState(pageId, versionNumber, updatedDate);
        return "redirect:/page/template/templateList.jsp";
    }

    /**
     * 删除模板
     *
     * @param pageId
     * @param versionNumber
     * @return
     */
    @RequestMapping("/deteleTemplate")
    public String deteleTemplate(String pageId, String versionNumber) {
        templateService.deleteTemplate(pageId, versionNumber);
        return "redirect:/page/template/templateList.jsp";
    }

    /**
     * 查询一条模板
     *
     * @param pageId
     * @param versionNumber
     * @return
     */
    @RequestMapping("/selectTemplatePageIdVersionNumber")
    @ResponseBody
    public Map<String, Object> selectTemplatePageIdVersionNumber(String pageId, String versionNumber) {
        Map<String, Object> map = new HashMap<>();
        List<Template> templates = templateService.selectTemplatem(pageId, versionNumber);
        List<AnnotationFields> patient = queryFieldAnnotationsService.selectPatientInformationSheet();
        map.put("patient", patient);
        map.put("templates", templates);
        return map;
    }
}
